package Test.park;

import java.util.*;

//import org.junit.jupiter.api.Test;

public class Main {

	// 记录停车位状态 
	// 停车位<车位号码，车位状态>
	Map<Integer, Boolean> parkMap = new HashMap<Integer, Boolean>();
	// 用于记录具体的停车记录，key为date是为了区分同一辆车在同一个车位停超过一次的情况
	Map<Date, ParkRecordModel> recordMap = new LinkedHashMap<Date, ParkRecordModel>();
	static int initHour = 0;
	static int endHour = 0;

	public static void main(String[] args) {
		Main m = new Main();
		m.print();

		Scanner scanner = new Scanner(System.in);
		System.out.println("输入初始化停车位的数量：");
		String numString = scanner.nextLine();
		int numLot = Integer.valueOf(numString);
		m.initParkingLot(numLot);
		m.stateOfAll();
		while (true) {
			System.out.println("请输入操作：");
			String string = scanner.nextLine();
			int operation = Integer.valueOf(string);
			if (operation == 0) {
				break;
			}

			while (operation != 0) {
				if (operation == 1) {// 停车
					System.out.println("请输入车牌号：");
					String licenseString = scanner.nextLine();
					System.out.println("请输入车辆高度：");
					String h = scanner.nextLine();
					int high = Integer.valueOf(h);

					if (high > 3 || licenseString == null) {
						System.out.println("您的车辆不符合停车场要求");
						break;
					}
					System.out.println("输入所停在的车位：");
					String string2 = scanner.nextLine();
					int num = Integer.valueOf(string2);

					if (num > numLot) {
						System.out.println("没有此停车位，重新输入");
						break;
					}
					if (m.parkMap.get(num) == false) {
						System.out.println("当前车位已被占用！");
						break;
					}
					Date startDate = new Date();

					m.ParkCar(licenseString, num, startDate);
					System.out.println(licenseString + " 号车---停车完成");
					break;
				} else if (operation == 2) {
					System.out.println("取车请输入您的车位号：");
					String s = scanner.nextLine();
					int num = Integer.valueOf(s);
					if (m.parkMap.get(num) == true) {
						System.out.println("您的车不在该车位：");
						break;
					}
					System.out.println("请输入您的车牌号码：");
					String caLlicense = scanner.nextLine();
					Date endDate = new Date();
					m.getCar(num, caLlicense, endDate);
					break;
				} else if (operation == 3) {
					m.stateOfAll();
					break;
				} else if (operation == 4) {
					m.findAll();
					break;
				}
			}

		}

		scanner.close();
	}

	public void print() {
		System.out.println("--------停车场管理 ---------");
		System.out.println("--------1:停车      ---------");
		System.out.println("--------2:取车      ---------");
		System.out.println("--------3:查询当前所有车位状态-");
		System.out.println("--------4:查询所有停车收费记录--");
		System.out.println("--------0:退出      ---------");
	}

	// 停车位初始化
	public void initParkingLot(int n) {
		for (int i = 1; i <= n; i++) {
			parkMap.put(i, true);
		}
		System.out.println("本停车场共有" + n + "个车位");

	}

	// 停车
	// @Test
	public void ParkCar(String licenseString, int num, Date startDate) {

		parkMap.put(num, false);
		// 停车时即创建停车记录，取车时间设为null
		ParkRecordModel p = new ParkRecordModel(num, licenseString, startDate, null);
		recordMap.put(startDate, p);
		Date date = new Date();
		System.out.println("当前时间是：" + date.toLocaleString());

	}

	// 取车
	public void getCar(int parkId, String caLlicense, Date endDate) {
		boolean flag = false;
		//判断当前车位所停的车牌号是否跟输入的车牌号一致
		for (Map.Entry<Date, ParkRecordModel> entry : recordMap.entrySet()) {
			if(entry.getValue().getEndTime()==null&&entry.getValue().getCarId().equals(caLlicense)) {
				parkMap.put(parkId, true);
				flag = true;
			}
		}
		if(flag==false) {
			System.out.println("该车位停的车的车牌号与您的车牌号不符！");
			return;
		}
		
		
		Date initDate = null;
		// 还车时，修改对应的停车记录，将取车时间修改
		ParkRecordModel pModel = new ParkRecordModel(parkId, caLlicense);
		for (Map.Entry<Date, ParkRecordModel> entry : recordMap.entrySet()) {
			if (entry.getValue().equals(pModel) && entry.getValue().getEndTime() == null) {
				
				entry.getValue().setEndTime(endDate);
				initDate = entry.getKey();
				// 记录停车费
				long start = initDate.getTime();
				int startHour = (int) (start / 1000);
				long end = endDate.getTime();
				int endHour = (int) (end / 1000);
				double res = (endHour - startHour) * 2;
				// 把收费情况更新到记录中
				entry.getValue().setMoney(res);
				recordMap.put(initDate, entry.getValue());

			}

		}

		System.out.println("取车时间：" + endDate.toLocaleString());

		System.out.println(parkId + " 号车位 " + " 车牌号为：" + caLlicense + " ---取车成功！");

	}

	// 查询所有车位的情况  
	public void stateOfAll() {
		System.out.println("当前停车位的情况：");
		for (Integer i : parkMap.keySet()) {
			if (parkMap.get(i) == true) {
				System.out.println(i + "号车位当前空闲");
			} else {
				System.out.println(i + "号车位当前已占");
			}
		}
	}

	// 查询统计全部的收费记录
	public void findAll() {

		System.out.println("全部收费记录如下：");
		int i = 1;
		for (Map.Entry<Date, ParkRecordModel> entry : recordMap.entrySet()) {
			if (entry.getValue().getEndTime() != null) {
				System.out.println(i + "." + "车牌号：" + entry.getValue().getCarId() + " 车位号："
						+ entry.getValue().getParkId() + " 停车时间：" + entry.getKey().toLocaleString() + " 取车时间："
						+ entry.getValue().getEndTime().toLocaleString() + " 收费：" + entry.getValue().getMoney() + "￥");
				i++;
			}

		}
	}
}
